<?php

class IptvprogramListController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
		array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
		),
		array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','admin'),
				'users'=>array('@'),
		),
		array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
		),
		array('deny',  // deny all users
				'users'=>array('*'),
		),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model=IptvLink::model()->findByAttributes(array('program_id'=>$id));
		$id= $model->program_id;
		$this->redirect(array('iptvLink/admin1','program_id'=>$id));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$late=yii::app()->request->getParam('late');
		$popu=yii::app()->request->getParam('popu');
		$reco=yii::app()->request->getParam('reco');

		$model=new IptvprogramList;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['IptvprogramList']))
		{
			$model->attributes=$_POST['IptvprogramList'];
			$category=$_POST['category'];
			if($category!=null){
				$model->category=implode('|',$category);
			}
			$package_id=$_POST['package_id'];
			if($package_id!=null){
				$model->package_id=implode('|',$package_id);
			}
			if($late!=null)
			{
				$model->latest=$late;
			}
			if($popu!=null)
			{
				$model->popular=$popu;
			}
			if($reco!=null)
			{
				$model->recommend=$reco;
			}

			$model->pic_link_small=CUploadedFile::getInstance($model,'pic_link_small');
			$model->pic_link_large=CUploadedFile::getInstance($model,'pic_link_large');

				
			if($model->pic_link_small)
			{
				$newsmallimg = 'pic_link_small_'.time().'_'.rand(1, 9999).'.'.$model->pic_link_small->extensionName;
				//根据时间戳重命名文件名,extensionName是获取文件的扩展名
				$model->pic_link_small->saveAs('./uploadimg/'.$newsmallimg);
				$model->pic_link_small = Yii::app()->request->hostInfo.Yii::app()->homeUrl.'uploadimg/'.$newsmallimg;
				//将image属性重新命名
			}
			if($model->pic_link_large)
			{
				$newlargeimg = 'pic_link_large_'.time().'_'.rand(1, 9999).'.'.$model->pic_link_large->extensionName;
				$model->pic_link_large->saveAs('./uploadimg/'.$newlargeimg);
				$model->pic_link_large = Yii::app()->request->hostInfo.Yii::app()->homeUrl.'uploadimg/'.$newlargeimg;
			}
			//$model->addtime = time();
			if($model->save())

			$this->redirect(array('admin','id'=>$model->program_id));


		}

		$this->render('create',array(
			'model'=>$model,
		));

	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		$late=yii::app()->request->getParam('late');
		$popu=yii::app()->request->getParam('popu');
		$reco=yii::app()->request->getParam('reco');
			
		if(isset($_POST['IptvprogramList']))
		{
			$model->attributes=$_POST['IptvprogramList'];
			$category=$_POST['category'];
			if($category!=null){
				$model->category=implode('|',$category);
			}
			$package_id=$_POST['package_id'];
			if($package_id!=null){
				$model->package_id=implode('|',$package_id);
			}
			if($late!=1)
			{
				$model->latest=0;
			}
			else
			{
				$model->latest=$late;}
				if($popu!=1)
				{
					$model->popular=0;
				}else
				{  $model->popular=$popu;}
				if($reco!=1)
				{
					$model->recommend=0;
				}else{ 
					$model->recommend=$reco;
				}
			 $model->pic_link_small=CUploadedFile::getInstance($model,'pic_link_small');
			 $model->pic_link_large=CUploadedFile::getInstance($model,'pic_link_large');

			 	
			 if($model->pic_link_small)
			 {
			 	$newsmallimg = 'pic_link_small_'.time().'_'.rand(1, 9999).'.'.$model->pic_link_small->extensionName;
			 	//根据时间戳重命名文件名,extensionName是获取文件的扩展名
			 	$model->pic_link_small->saveAs('./uploadimg/'.$newsmallimg);
			 	$model->pic_link_small = Yii::app()->request->hostInfo.Yii::app()->homeUrl.'uploadimg/'.$newsmallimg;
			 	//将image属性重新命名
			 }else {
			 	$model->pic_link_small = $_POST['pic_link_small2'];
			 }
			 if($model->pic_link_large)
			 {
			 	$newlargeimg = 'pic_link_large_'.time().'_'.rand(1, 9999).'.'.$model->pic_link_large->extensionName;
			 	$model->pic_link_large->saveAs('./uploadimg/'.$newlargeimg);
			 	$model->pic_link_large = Yii::app()->request->hostInfo.Yii::app()->homeUrl.'uploadimg/'.$newlargeimg;
			 }else {
			 	$model->pic_link_large = $_POST['pic_link_large2'];
			 }
			 //$model->addtime = time();
			 if($model->save())

				$this->redirect(array('admin','id'=>$model->program_id));
					
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
		$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('IptvprogramList');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$type_id=yii::app()->request->getParam('type_id');
		$model=new IptvprogramList('search');
		$model->unsetAttributes();  // clear any default values
		if($type_id!=null)
		{
			$model->type_id=$type_id;
		}
		else
		{
			if(isset($_GET['IptvprogramList']))
			$model->attributes=$_GET['IptvprogramList'];
		}
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return IptvprogramList the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=IptvprogramList::model()->findByPk($id);
		if($model===null)
		throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param IptvprogramList $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='iptvprogram-list-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}





}
